﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BayesDraw
{
    class RNode:PictureBox
    {
        private string _strMsg = string.Empty;
        private Graphics _parentGraphics;

        public static List<RNode> AllNodes = new List<RNode>(); 

        public List<RNode> TargetNodes = new List<RNode>();
        public List<RNode> OriginNodes = new List<RNode>(); 

        public List<Arrow> OutArrows = new List<Arrow>();
        public List<Arrow> InArrows = new List<Arrow>(); 

        internal RNode(string name,Graphics parentGraphics)
        {
            InitRNode();
            this.BackColor = Color.Transparent;
            //this.BackColor = Color.BurlyWood;
            this.ForeColor = Color.Transparent;
            //this.ForeColor = Color.Aqua;
            this.Paint += RNode_Paint;
            this.MouseMove += RNode_MouseMove;
            this.MouseDown += RNode_MouseDown;

            this.Name = name;
            _parentGraphics = parentGraphics;

            AllNodes.Add(this);
        }

        public Point _mouseOffset;//鼠标位移偏移量

        public event Action MoveEvent;

        void InitRNode()
        {
            Image = global::BayesDraw.Properties.Resources.Round_Blue_64;
            Size = new System.Drawing.Size(49, 49);
            SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;

            //将PictureBox绘制成圆形
            GraphicsPath gp = new GraphicsPath();
            gp.AddEllipse(ClientRectangle);
            Region region = new Region(gp);
            this.Region = region;
            gp.Dispose();
            //region.Dispose();
        }

        void RNode_MouseDown(object sender, MouseEventArgs e)
        {
            _mouseOffset = new Point(-e.X, -e.Y);
        }

        void RNode_MouseMove(object sender, MouseEventArgs e)
        {
            RNode pb = (RNode)sender;
            pb.Cursor = Cursors.Arrow;//设置拖动时鼠标箭头
            if (e.Button == MouseButtons.Left)
            {
                Point mousePos = Control.MousePosition;
                mousePos.Offset(pb._mouseOffset.X, pb._mouseOffset.Y);//设置偏移
                pb.Location = pb.Parent.PointToClient(mousePos);
            }
            //MoveEvent();
        }


        void RNode_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Font fnt = new Font("Arial", 10);
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;

            g.DrawString(this.Name, fnt, new SolidBrush(Color.Tomato), this.ClientRectangle, sf);

        }

        /// <summary>
        /// 更改名称
        /// </summary>
        /// <param name="str">内容</param>
        public void ChangeName(string name)
        {
            this.Name = name;
            Invalidate();
        }

        public void AddTargetNode(RNode targetNode,Arrow arrow)
        {
            this.TargetNodes.Add(targetNode);
            this.OutArrows.Add(arrow);
        }

        public void AddTargetNode(RNode targetNode)
        {
            
        }

        public void AddOriginNode(RNode originNode, Arrow arrow)
        {
            this.OriginNodes.Add(originNode);
            this.InArrows.Add(arrow);
        }

        public void AddOriginNode(RNode originNode)
        {
            
        }

        public void RemoveTargetNode(RNode targetNode, Arrow arrow)
        {
            this.TargetNodes.Remove(targetNode);
            this.OutArrows.Remove(arrow);
        }

        public void RemoveTargetNode(RNode targetNode)
        {
            
        }

        public void RemoveOriginNode(RNode originNode, Arrow arrow)
        {
            this.OriginNodes.Remove(originNode);
            this.InArrows.Remove(arrow);
        }

        public void RemoveOriginNode(RNode originNode)
        {
            
        }



        public Point GetMidPoint()
        {
            Point p = new Point();
            p.X = this.Location.X + this.Width/2;
            p.Y = this.Location.Y + this.Height/2;
            return p;
        }

    }
}
